﻿using System;
using System.Linq;

namespace TLang.Ast
{
    using System.Collections.Generic;
    using Values;

    public class Delimeter : Node
    {
        public readonly String Shape;


        public Delimeter(String shape, String file, int start, int end, int line, int col) : base(file, start, end, line, col)
        {
            this.Shape = shape;
        }


        public override Value Interp(Scope s)
        {
            return null;
        }

        public override Value Typecheck(Scope s)
        {
            return null;
        }


        // all delimeters
        private static readonly ISet<String> delims = new HashSet<String>();

        // map open delimeters to their matched closing ones
        private static readonly Map<String, String> delimMap = new Map<string, string>();


        public static void AddDelimiterPair(String open, String close)
        {
            delims.Add(open);
            delims.Add(close);
            delimMap.Put(open, close);
        }


        public static void AddDelimiter(String delim)
        {
            delims.Add(delim);
        }


        public static bool IsDelimiter(char c)
        {
            return delims.Contains(c.ToString());
        }

        public static bool IsOpen(Node c)
        {
            return (c is Delimeter) && delimMap.Keys.Contains(((Delimeter)c).Shape);
        }


        public static bool IsClose(Node c)
        {
            return (c is Delimeter) && delimMap.Values.Contains(((Delimeter)c).Shape);
        }


        public static bool Match(Node open, Node close)
        {
            if (!(open is Delimeter) || !(close is Delimeter))
            {
                return false;
            }
            String matched = delimMap.Get(((Delimeter)open).Shape);
            return matched != null && matched.Equals(((Delimeter)close).Shape);
        }


        public override String ToString()
        {
            return Shape;
        }
    }
}
